perf: specialize OnceMap for read-mostly workloads - #241
Closed
tisonkun wants to merge 1 commit into
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PendingandReadyentries directly in one hash table, with waiter coordination allocated only while a value is being initialized.OnceCelland semaphore dependency from theonce-mapfeature without adding dependencies or unsafe code.removeordiscard.Design Notes
A ready entry now retains only its key and
Arc<V>. A pending entry owns anInitializationcontaining the transient wait set; successful publication changes that table entry in place, while failure removes it and wakes one of the joined callers to retry.hashbrown::HashTableremains as the raw index. Once a non-Clonekey has moved into the table, the initializer must locate its exact generation using a precomputed hash andArcidentity; stablestd::collections::HashMapdoes not expose the raw-hash lookup needed to do this without duplicating the key or scanning the map.Removing permanent coordination has a deliberate cold-path cost: successful first initialization allocates the stored
Arc<V>and transitions the table entry after the computation. This favors the intended initialize-once/read-many workload rather than cold insertion throughput.Debugreports entry and pending counts so it does not call user formatting code while holding the table lock.Benchmark
The table reports the median of four isolated runs of the current OnceMap benchmark through the Cargo benchmark harness on the same machine.
maincompute, same key, 1 threadcompute, 64 distributed keys, 1 threadcompute, 1024 distributed keys, 1 threadget, 1024 distributed keys, 1 threadremovehit, 1024 entriesHighly contended 32-thread results varied by path within roughly a 7.6% deterioration to an 11.4% optimization, so this change does not claim a general contention win.
Validation
cargo x checkcargo x testcargo x lint